// Deferred shading, store the eye coordinates in a float texture
uniform samplerRectShadow shadowMap;
varying vec4 ec4; 
varying vec3 normal; 
uniform mat4 shadowMat;
uniform vec2 screenSize;

void main (void)
{
    vec4 shadowCoord = ec4 * shadowMat;
    shadowCoord = shadowCoord / shadowCoord.w;
	shadowCoord.s *= screenSize.x;
	shadowCoord.t *= screenSize.y;
	//shadowCoord.z -= 0.001;
	// TODO more/better sampling
	float shadow = shadowRect(shadowMap, shadowCoord).x;
	shadow += shadowRect(shadowMap, shadowCoord + vec4(1,1,0,0)).x;
	shadow += shadowRect(shadowMap, shadowCoord + vec4(-1,-1,0,0)).x;
	shadow += shadowRect(shadowMap, shadowCoord + vec4(1,-1,0,0)).x;
	shadow += shadowRect(shadowMap, shadowCoord + vec4(-1,1,0,0)).x;
	shadow += shadowRect(shadowMap, shadowCoord + vec4(2,0,0,0)).x;
	shadow += shadowRect(shadowMap, shadowCoord + vec4(-2,0,0,0)).x;
	shadow += shadowRect(shadowMap, shadowCoord + vec4(0,-2,0,0)).x;
	shadow += shadowRect(shadowMap, shadowCoord + vec4(0,2,0,0)).x;
	shadow/=9;

	gl_FragData[0] = vec4(vec3(ec4), shadow);
	gl_FragData[1] = vec4(normal, 1.0);
	gl_FragData[2] = gl_FrontMaterial.diffuse;
}

